/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.p10.miage.gestionabsence.model;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Sofian Seid
 */
public class FaqRequestProcessor extends org.apache.struts.action.RequestProcessor{

    public FaqRequestProcessor() {
        super();
    } 
    
    // On verifie si l'utilsateur est bien connecté 
    protected boolean isUserConnected(HttpServletRequest request) {
        if (request != null) {
            HttpSession session = request.getSession(false);
            if (session != null) {
                    Utilisateur  utilisateur =  (Utilisateur) session.getAttribute("utilisateur");
                    return utilisateur != null;
            }
            return false;
        }
        return false;
    }

    // On vérifie si la session a expirée : la session expire si l'id de la session n'est pas valide
    protected boolean isSessionValid(HttpServletRequest request) throws  ServletException {
            if (request != null) {
                return request.isRequestedSessionIdValid();
            }
            return  false;
    }

    @Override
    protected boolean processRoles(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) 
    throws java.io.IOException, javax.servlet.ServletException {

        ActionMessages messages = new ActionMessages();
        if (isSessionValid(request)) {
            if (isUserConnected(request)) {
                return true;
            } 
            else {
                messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("erreurs.connection.required"));
            }
        } 
        else {
            messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("erreurs.session.expired"));
        }
        
        request.getRequestDispatcher ("/error.jsp").forward(request,response);

        return false;
    }
  
}
